package com.google.code.fluentjoiner.io;

import com.google.code.fluentjoiner.AbstractSupplierBuilder;
import com.google.code.fluentjoiner.Consumer;
import com.google.code.fluentjoiner.StringSupplierBuilder;
import com.google.code.fluentjoiner.SupplierBuilder;
import com.google.common.base.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class FileObjectSupplierBuilder<T extends FileObject> extends AbstractSupplierBuilder<T>
{
    private static final Logger LOGGER = LoggerFactory.getLogger(FileObjectSupplierBuilder.class);    

    public SupplierBuilder<byte[]> bytesFromFile()
    {
        return transform(FileObjectFunctions.FILEOBJECT_TO_BYTES);
    }

    public StringSupplierBuilder textFromFile()
    {
        return transform(FileObjectFunctions.FILEOBJECT_TO_STRING).toStringSupplierBuilder();
    }

    public SupplierBuilder<Consumer<byte[]>> toConsumer()
    {
        final Supplier<T> fileObjectSupplier = getSupplier();
        return new AbstractSupplierBuilder<Consumer<byte[]>>()
        {
            @Override
            protected Consumer<byte[]> getSupplierResult()
            {
                return new Consumer<byte[]>()
                {
                    @Override
                    public void consume(final byte[] valueToConsume)
                    {
                        fileObjectSupplier.get().writeContent(valueToConsume);
                    }
                };
            }
        };
    }
}